﻿using Sidvall.Serialization;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Sidvall.UI.Wpf.Converters
{
    public abstract class ValueToVisibilityConverterBase : IValueConverter
    {
        #region Public Members

        protected abstract object VisibilityToValue(object value, Type targetType, object parameter, CultureInfo culture);

        #region Collapsed

        private bool _Collapsed = true;
        public bool Collapsed
        {
            get
            {
                return _Collapsed;
            }
            set
            {
                _Collapsed = value;
            }
        }

        #endregion
        #region Inverted

        private bool _Inverted = false;
        public bool Inverted
        {
            get
            {
                return _Inverted;
            }
            set
            {
                _Inverted = value;
            }
        }

        #endregion
        #region Not

        private bool _Not = false;
        public bool Not
        {
            get
            {
                return _Not;
            }
            set
            {
                _Not = value;
            }
        }

        #endregion

        #region Convert

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!this.Inverted)
                return ValueToVisibility(value, targetType, parameter, culture);
            else
                return VisibilityToValue(value, targetType, parameter, culture);
        }

        #endregion
        #region ConvertBack

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!this.Inverted)
                return VisibilityToValue(value, targetType, parameter, culture);
            else
                return ValueToVisibility(value, targetType, parameter, culture);
        }

        #endregion

        #region ValueToVisibility

        protected virtual Visibility ValueToVisibility(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString(string.Empty).Equals(parameter.ToString(string.Empty), true, StringComparison.OrdinalIgnoreCase))
            {
                if (!this.Not)
                {
                    return Visibility.Visible;
                }
                else
                {
                    if (this.Collapsed)
                        return Visibility.Collapsed;
                    else
                        return Visibility.Hidden;
                }
            }
            else
            {
                if (!this.Not)
                {
                    if (this.Collapsed)
                        return Visibility.Collapsed;
                    else
                        return Visibility.Hidden;
                }
                else
                {
                    return Visibility.Visible;
                }
            }
        }

        #endregion

        #endregion
    }
}
